<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>继承</title>
    <!--
        原型属性关联形成原型链：
        几乎所有对象都有prototype属性，该属性指向一个原型对象。
        原型对象可以作为一个模板，新对象可以从中获得原始的属性和方法。
        每个原型对象也有自己的prototype属性，该属性指向另一个原型对象，从而共享它的属性和方法。
    -->
</head>
<body>
<script>
    //方式一：借用构造函数(经典继承)
    function Person(name){
        this.name = name;
    }
    function Worker(name, salary){
        Person.call(this,name);//this.name = name(×)  Person.call(name) （√） 对象冒充
        this.salary = salary;
    }
    var obj3 = new Worker("赵云", 100);
    console.log(obj3);
    console.log(obj3.name, obj3.salary);

    //方式二 组合继承(伪经典继承)
    function Person(name){
        this.name = name;
    }
    Person.prototype.run = function(){
        console.log("跑")
    }

    function Worker(name,salary){
        Person.call(this, name);
        this.salary = salary;
    }
    Worker.prototype = new Person();//利用原型获取方法
    var obj4 = new Worker("赵云", 100);
    console.log(obj4);

    //方法三：原型式继承
    var person1 = {
        name: "刘备",
        sex: "男",
        run:function(){
            console.log("跑");
        }
    }
    console.log(person1);
    var person2 = Object.create(person1);
    console.log(person2);

    //方式四：寄生式继承 了解一下 经常使用前三种方法
    function createNewPerson(person){
        var temp = new Object(person);
        temp.jump = function(){
            console.log("跳");
        }
        return temp;
    }
    var person1 = {
        name: "刘备",
        sex: "男",
        run:function(){
            console.log("跑");  
        }
    }

    var person2 =createNewPerson(person1);
    console.log(person2);
    //方式五：寄生组合式继承

</script>    
</body>
</html>